package de.medizin.uni.halle.irm;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.SharedPreferences;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.os.StrictMode;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.text.InputType;
import android.view.ViewGroup.LayoutParams;
import android.widget.EditText;
import android.widget.LinearLayout;

import com.actionbarsherlock.app.ActionBar;
import com.actionbarsherlock.app.ActionBar.Tab;
import com.actionbarsherlock.app.SherlockFragmentActivity;
import com.actionbarsherlock.view.Menu;
import com.actionbarsherlock.view.MenuInflater;
import com.actionbarsherlock.view.MenuItem;

public class BornActivity extends SherlockFragmentActivity implements ActionBar.TabListener {
    /** Called when the activity is first created. */
    private static int position = 0;
    private Fragment mFragment;
    private DatabaseHelper dbhelper;
    SQLiteDatabase db;
    private ActionBar actionbar;
    
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
                
        setContentView(R.layout.main);
        
        showLoginDialog(this);

        StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
        StrictMode.setThreadPolicy(policy);
    	dbhelper = new DatabaseHelper(getApplicationContext());
    	db = dbhelper.getWritableDatabase();
    	
    	createSettings(this);
    	
       	actionbar = getSupportActionBar();
        //actionbar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM | ActionBar.DISPLAY_USE_LOGO);
       	actionbar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
        actionbar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
        actionbar.setDisplayShowHomeEnabled(false);

        actionbar.addTab(actionbar.newTab().setText(R.string.overview).setTabListener(this));
        actionbar.addTab(actionbar.newTab().setText(R.string.patients).setTabListener(this));
        actionbar.addTab(actionbar.newTab().setText(R.string.history).setTabListener(this)); 
        actionbar.addTab(actionbar.newTab().setText(R.string.emails).setTabListener(this));
        actionbar.addTab(actionbar.newTab().setText(R.string.sms).setTabListener(this));
        actionbar.addTab(actionbar.newTab().setText(R.string.calls).setTabListener(this));
        actionbar.addTab(actionbar.newTab().setText(R.string.letters).setTabListener(this)); 
    }
    
    private void showLoginDialog(final BornActivity bornActivity) {
       	final AlertDialog.Builder alert = new AlertDialog.Builder(this);
       	alert.setCancelable(false);
    	alert.setTitle("Bitte geben sie ihre Kennung ein");
        final EditText username = new EditText(this);
        username.setHint("Nutzername");
        username.setInputType(InputType.TYPE_TEXT_VARIATION_PASSWORD);      
        final EditText password = new EditText(this);
        password.setHint("Passwort");
        password.setInputType(InputType.TYPE_TEXT_VARIATION_PASSWORD); 
        LayoutParams editTextLayoutParams = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
        username.setLayoutParams(editTextLayoutParams);
        password.setLayoutParams(editTextLayoutParams);
        LinearLayout layout = new LinearLayout(this);
        layout.setOrientation(LinearLayout.VERTICAL);
        layout.addView(username);
        layout.addView(password);  
        alert.setView(layout);

        alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int whichButton) {  
            		if(username.getText().toString().equals(""))
            		{
                    	dialog.cancel(); 
              	   	  	bornActivity.finish();            			
            		}
            		else {
	                	SharedPreferences settings = getSharedPreferences("preferences", 0);
	                	SharedPreferences.Editor editor = settings.edit();
	                	editor.putString("user", username.getText().toString());
	                	editor.commit();
	            	   	dialog.cancel();   
            		}

            	} 	
            });
        alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() { 
            public void onClick(DialogInterface dialog, int whichButton) { 
            	dialog.cancel(); 
      	   	  	bornActivity.finish();
      	   	  }
        }); 
        alert.show();	
}

	private void createSettings(BornActivity bornActivity) {
    	/*SQLiteDatabase database = openOrCreateDatabase("born", Context.MODE_PRIVATE, null);        
        new ExampleData(database);
        database.close();*/
        
	}

	@Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getSupportMenuInflater();
        inflater.inflate(R.menu.menu, menu);
        return true;
    }
    @Override
    public boolean onOptionsItemSelected(final MenuItem item) {
    	final AlertDialog.Builder alert = new AlertDialog.Builder(this);
    	alert.setTitle("Bitte Administrator Passwort eingeben");
        final EditText input = new EditText(this);
        input.setInputType(InputType.TYPE_TEXT_VARIATION_PASSWORD);
        alert.setView(input);
        alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int whichButton) {            	
            	if(input.getText().toString().equals("test")){
                	dialog.cancel();
                	if(item.getItemId()==R.id.menu_settings) 
                	{
                		FragmentManager fm = getSupportFragmentManager();
                		FragmentTransaction ft = fm.beginTransaction();
                		ft.detach(mFragment);
                		mFragment = new ViewPagerFragment(0, 9);
                		ft.add(R.id.container, mFragment);
                		ft.commit();
                	} else {
                		FragmentManager fm2 = getSupportFragmentManager();
                		FragmentTransaction ft2 = fm2.beginTransaction();
                		ft2.detach(mFragment);
                		mFragment = new ImpExpFragment();
                		ft2.add(R.id.container, mFragment);
                		ft2.commit();                		  
                	}
            	} 	
            }
        });
        alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() { 
            public void onClick(DialogInterface dialog, int whichButton) { 
              dialog.cancel(); 
            } 
          }); 
        alert.show();
    	return true;
    }


	public static void setPosition(int p){
		position = p;
	}

	public void onTabSelected(Tab arg0,FragmentTransaction arg1) {
		int fragment = arg0.getPosition();
		if(fragment==0)
			mFragment = new OverviewFragment();
		if(fragment==1)
			mFragment = new PatientsFragment();
		if(fragment==2)
			mFragment = new HistoryFragment();
		if(fragment==3)
			mFragment = new ViewPagerFragment(position, fragment);
		if(fragment==4)
			mFragment = new ViewPagerFragment(position, fragment);
		if(fragment==5)			
			mFragment = new ViewPagerFragment(position, fragment);
		if(fragment==6)
			mFragment = new ViewPagerFragment(position, fragment);

		
		arg1.add(R.id.container, mFragment);
	}

	public void onTabReselected(Tab arg0, FragmentTransaction arg1) {
		arg1.detach(mFragment);
		int fragment = arg0.getPosition();
		if(fragment==0)
			mFragment = new OverviewFragment();
		if(fragment==1)
			mFragment = new PatientsFragment();
		if(fragment==2)
			mFragment = new HistoryFragment();
		if(fragment==3)
			mFragment = new ViewPagerFragment(position, fragment);
		if(fragment==4)
			mFragment = new ViewPagerFragment(position, fragment);
		if(fragment==5)
			mFragment = new ViewPagerFragment(position, fragment);
		if(fragment==6)
			mFragment = new ViewPagerFragment(position, fragment);

		arg1.add(R.id.container, mFragment);
	}

	public void onTabUnselected(Tab tab, FragmentTransaction ft) {
		ft.detach(mFragment);
	}
	
	@Override
	public void onDestroy(){
		super.onDestroy();
		dbhelper.close();
	}
}
